package com.pan.cuiniubi.create.dao;

import com.pan.cuiniubi.entity.*;
import com.pan.cuiniubi.enumn.LaminaEnum;
import com.pan.cuiniubi.enumn.OperationEnum;
import com.pan.cuiniubi.utils.ReadYamlTool;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CreateUpdateDaoMapping extends CreateDaoMappingImpl {

    public <T> T copy2(T obj,String parameterName){
        Class type = obj.getClass();
        Field[] fields = type.getDeclaredFields();
        T newObj;
        try {
            newObj = (T) type.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        Arrays.stream(fields).forEach(field -> {
            field.setAccessible(true);
            try {
                Object t = field.get(obj);
                if (parameterName.equals(field.getName())){
                    field.set(newObj, (String)t+"2");
                }else {
                    field.set(newObj, t);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        });
        return newObj;
    }
    @Override
    public List<MethodInfo> getMethodInfo(Table table) {
        List<Member> memberList = table.getMemberList();
        Member primaryMember = memberList.stream().filter(member -> member.getIsPrimary()).findFirst().get();
        List<MethodInfo> result = memberList.stream().map(member -> {
            return new MethodInfo().setMethodName("update"+member.getMemberName().substring(0,1).toUpperCase()
                            + member.getMemberName().substring(1))
                    .setReturnStr(Integer.class.getName())
                    .setParameterList(primaryMember,member.getIsPrimary()?this
                            .copy2(member,"memberName"):member);
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public DaoInfo getDaoInfo(Table table) {
        DaoInfo daoInfo = new DaoInfo();
        daoInfo.setPackageStr(getPackageStr())
                .setMethodInfoList(getMethodInfo(table))
                .setImportStr(getImportString(table))
                .setClassName(getClassName(table.getTableName(), OperationEnum.update));
        return daoInfo;
    }


    public List<SqlInfo> getSqlInfo(Table table){
        String tableName =  table.getTableName();
        String path = (String) ReadYamlTool.getInstance().getMapLaminaValue(LaminaEnum.entity,"path");
        List<Member> memberList = table.getMemberList();
        String primaryName = memberList.stream().filter(Member::getIsPrimary).findFirst().get().getMemberName();
        String whereSqlStr = " where "+primaryName+"=#{"+primaryName+"}";
        List<SqlInfo> sqlInfoList = memberList.stream().map(member -> {
            SqlInfo sqlInfo = new SqlInfo();
            sqlInfo.setOpt(OperationEnum.update.name());
//            sqlInfo.setResult(path+"."+tableName);
            sqlInfo.setId("update"+member.getMemberName().substring(0,1).toUpperCase()
                    +member.getMemberName().substring(1));
            sqlInfo.setSql("update "+tableName+" set "+member.getMemberName()+"=#{"+member.getMemberName()+"}"+whereSqlStr);
            return sqlInfo;
        }).collect(Collectors.toList());
        return sqlInfoList;
    }
    public MappingInfo getMappingInfo(Table table){
        MappingInfo mappingInfo = new MappingInfo();
        mappingInfo.setNamespace(getNamespace(table.getTableName(),OperationEnum.update));
        mappingInfo.setSqlInfoList(getSqlInfo(table));
        String className = getClassName(table.getTableName(),OperationEnum.update);
        mappingInfo.setClassName(className);
        return mappingInfo;
    }
}
